package cn.dylan.org.controller;

import cn.dylan.basic.util.JsonResult;
import cn.dylan.basic.util.PageList;
import cn.dylan.org.domain.Employee;
import cn.dylan.org.query.EmployeeQuery;
import cn.dylan.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description="相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     *
     * @return
     */
    @ApiOperation(value = "查询所有" )
    @GetMapping
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    /**
     * GetMapping 请求
     * @requstMapping(value = "", method = "get")
     * @param id
     * @return
     */
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public Employee loadById(@PathVariable("id") Long id){
        return employeeService.LoadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一个部门" )
    public JsonResult delete(@PathVariable("id") Long id){
        System.out.println(id);
        try {
            employeeService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 修改和添加
     * @param employee
     * @return
     */
    @PostMapping
    @ApiOperation(value = "修改或者添加" )
    public JsonResult updateOrAdd(@RequestBody Employee employee){
        try{
            if(employee.getId()==null){
                //添加操作
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PutMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Employee> QueryPage(@RequestBody EmployeeQuery query){
        try {
            return employeeService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList();
        }
    }

    /**
     * 批量删除
     */
    @ApiOperation(value = "批量删除部门" )
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     *  get("/employee/list/"+shopId)
     * @return
     */
    @ApiOperation(value = "查询店铺所有员工" )
    @GetMapping("/list/{shopId}")
    public List<Employee> getShopEmployees(@PathVariable("shopId") Long shopId){
        return employeeService.getShopEmployees(shopId);
    }


}
